/// @tags: DP Dilworth
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

int constexpr N = 1e3 + 5;

int map[N][N];
LL dp[N][N];

inline int main() {
  int t;
  cin >> t;
  for (int n, m; t; --t) {
    memset(map, 0, sizeof(map));
    memset(dp, 0, sizeof(dp));
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) cin >> map[i][j];
    for (int i = 1; i <= n; ++i)
      for (int j = m; j > 0; --j)
        dp[i][j] = max(dp[i - 1][j + 1] + map[i][j], max(dp[i][j + 1], dp[i - 1][j]));
    cout << dp[n][1] << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P3974 [TJOI2015] 组合数学.in", "r", stdin);
  freopen("P3974 [TJOI2015] 组合数学.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
